package com.kmxd.ams.app.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.system.IConfI18nDetailSvc;
import com.kmxd.ams.client.system.dto.ConfI18nDetailAddDTO;
import com.kmxd.ams.client.system.dto.ConfI18nDetailEditDTO;
import com.kmxd.ams.client.system.dto.ConfI18nDetailQueryDTO;
import com.kmxd.ams.client.system.vo.ConfI18nDetailListVO;
import com.kmxd.ams.client.system.vo.ConfI18nDetailVO;
import com.kmxd.ams.core.common.constant.CacheConst;
import com.kmxd.ams.core.util.ConvertUtil;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.system.entity.ConfI18nDetail;
import com.kmxd.ams.infra.system.mapper.ConfI18nDetailMapper;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 国际化语言详情表 服务实现类
 *
 * @author ZuoLG
 * @since 2022-07-14
 */
@Service
@AllArgsConstructor
public class ConfI18nDetailSvcImpl extends ServiceImpl<ConfI18nDetailMapper, ConfI18nDetail>
    implements IConfI18nDetailSvc {

  private ConfI18nDetailMapper confI18nDetailMapper;

  @Override
  public Page<ConfI18nDetailListVO> selectPage(Page reqPage, ConfI18nDetailQueryDTO req) {
    LambdaQueryWrapper<ConfI18nDetail> queryWrapper =
        Wrappers.<ConfI18nDetail>lambdaQuery()
            .eq(Objects.nonNull(req.getConfId()), ConfI18nDetail::getConfId, req.getConfId())
            .eq(
                StrUtil.isNotBlank(req.getLangCode()),
                ConfI18nDetail::getLangCode,
                Optional.ofNullable(req.getLangCode()).orElse("").toLowerCase());
    Page<ConfI18nDetail> page = confI18nDetailMapper.selectPage(reqPage, queryWrapper);
    Page<ConfI18nDetailListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ConfI18nDetailListVO.class));
    return voPage;
  }

  @Override
  public List<ConfI18nDetailListVO> selectList(ConfI18nDetailQueryDTO req) {
    LambdaQueryWrapper<ConfI18nDetail> queryWrapper =
        Wrappers.<ConfI18nDetail>lambdaQuery()
            .eq(Objects.nonNull(req.getConfId()), ConfI18nDetail::getConfId, req.getConfId())
            .eq(
                StrUtil.isNotBlank(req.getLangCode()),
                ConfI18nDetail::getLangCode,
                Optional.ofNullable(req.getLangCode()).orElse("").toLowerCase());
    List<ConfI18nDetail> list = confI18nDetailMapper.selectList(queryWrapper);
    return ConvertUtil.toList(list, ConfI18nDetailListVO.class);
  }

  @CacheEvict(value = CacheConst.CACHE_PREFIX_CONF_I18N, allEntries = true)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ConfI18nDetailAddDTO req) {
    ConfI18nDetail entity = BeanUtil.copyProperties(req, ConfI18nDetail.class);
    entity.setLangCode(entity.getLangCode().toLowerCase());
    confI18nDetailMapper.insert(entity);
  }

  @CacheEvict(value = CacheConst.CACHE_PREFIX_CONF_I18N, allEntries = true)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ConfI18nDetailEditDTO req) {
    ConfI18nDetail entity = BeanUtil.copyProperties(req, ConfI18nDetail.class);
    entity.setLangCode(entity.getLangCode().toLowerCase());
    confI18nDetailMapper.updateById(entity);
  }

  @CacheEvict(value = CacheConst.CACHE_PREFIX_CONF_I18N, allEntries = true)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    confI18nDetailMapper.deleteBatchIds(idList);
  }

  @Override
  public ConfI18nDetailVO view(Long id) {
    ConfI18nDetail entity = confI18nDetailMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ConfI18nDetailVO.class);
  }
}
